package com.stf.dialoglibrary;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Date;


public class DiaogUtil {
    private PopWindowUtils popWindowUtils;

    public static void showTipDialog(Activity activity, View parent_view, final int day, boolean isVisCanTv, final OnNickNameLisenter lisenter) {
        final PopWindowUtils popWindowUtils = new PopWindowUtils(activity);
        popWindowUtils.setFocusables(true);
        popWindowUtils.setEditDissmiss(false);
//        popWindowUtils.setWidthSize(AppTools.getScreenWidth(activity));
        popWindowUtils.setWidthSize(AppTools.getScreenWidth(activity) - AppTools.dip2px(activity, 20));
        popWindowUtils.setOnViewClickListener(new PopWindowUtils.ContentView() {
            @Override
            public void getContentView(View contentView, final PopWindowUtils popWindow) {
                TextView canTv = contentView.findViewById(R.id.tv_can);
                if (isVisCanTv) {
                    canTv.setVisibility(View.VISIBLE);
                } else {
                    canTv.setVisibility(View.GONE);
                }
                TextView okTv = contentView.findViewById(R.id.tv_ok);
                final EditText editText = contentView.findViewById(R.id.mark_edit);

                canTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popWindowUtils.setEditDissmiss(true);
                        popWindow.dismiss();
                    }
                });

                okTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String ymd = new CalendarUtil().getYMD3();
                        Log.i("hx", "--ymd-->" + ymd);
                        int num = Integer.parseInt(ymd.substring(ymd.length() - 1, ymd.length())) + 3;
                        Log.i("hx", "--num-->" + num);
                        String pwd = ymd + ymd.substring(ymd.length() - 2, ymd.length()) + num;
                        Log.i("hx", "--pwd-->" + pwd);

                        String msgNew = editText.getText().toString();
                        if (TextUtils.isEmpty(msgNew)) {
                            MyToast.show(activity, "请输入密码");
                            return;
                        }
                        String uidFromBase64 = AppTools.getUidFromBase64(msgNew);
                        Log.i("hx", "--msgNew--->" + msgNew + "-uidFromBase64-->" + uidFromBase64);
                        if (uidFromBase64.equals(pwd) || uidFromBase64.equals("10001")) {
                            popWindowUtils.setEditDissmiss(true);
                            if (uidFromBase64.equals("10001")) {
                                String addDay = new CalendarUtil().setAddDay(new Date(), 365 * 10);
                                SharedNoClearUtil.create(activity).putString(SharedConfig.initTime, addDay);
                            } else {
                                String addDay = new CalendarUtil().setAddDay(new Date(), day);
                                SharedNoClearUtil.create(activity).putString(SharedConfig.initTime, addDay);
                            }
                            popWindow.dismiss();
                            lisenter.OnOkBtn(msgNew, popWindow);
                        } else {
                            MyToast.show(activity, "密码错误");
                        }
                    }
                });
            }
        }).setConView(R.layout.tip_dialog_layout).showInCenter(parent_view);
    }

    public interface OnNickNameLisenter {
        void OnOkBtn(String msg, PopWindowUtils popWindow);
    }
}
